package com.jia.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.blog.common.lang.R;
import com.jia.blog.entity.VideoDetail;
import com.jia.blog.entity.VideoType;
import com.jia.blog.service.VideoDetailService;
import com.jia.blog.service.VideoTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiajia
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/video-detail")
@Api(tags = "视频详情管理")
public class VideoDetailController {

    @Autowired
    VideoDetailService videoDetailService;

    @Autowired
    VideoTypeService videoTypeService;

    @GetMapping("/list/{typeId}")
    public R list(@PathVariable Long typeId) {
        VideoType videoType = videoTypeService.getById(typeId);
        String name = videoType.getName();
        QueryWrapper<VideoDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",typeId);
        wrapper.orderByAsc("sort");
        List<VideoDetail> list = videoDetailService.list(wrapper);
        return R.ok().data("name",name).data("data",list);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        VideoDetail data = videoDetailService.getById(id);
        return R.ok().data("data",data);
    }

    @PostMapping("/save")
    public R save(@RequestBody VideoDetail videoDetail) {
        videoDetailService.save(videoDetail);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody VideoDetail videoDetail) {
        videoDetailService.updateById(videoDetail);
        return R.ok();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id){
        videoDetailService.removeById(id);
        return R.ok();
    }
}
